package com.zim.util;

import com.zim.cache.Cache;
import com.zim.cache.SimpleCache;
import javafx.application.Platform;
import javafx.stage.Stage;

/**
 * 描述:
 *
 * @Author: ZimH
 * @Mobile: 182-5958-1506
 * @CreateDate: 2022/1/11 18:29
 * @Version: 1.0
 */

public class SimulationRedisUtil {
    private Cache cache;

    public SimulationRedisUtil() {
        this.cache = new SimpleCache();//模拟缓存
    }

    public void set(String key, Object object, long expired){
        cache.set(key,object,expired);
    }

    public <T> T get(String key){
        return cache.get(key);
    }

    public void set(String key, Object object){
        cache.set(key, object);
    }

    public void del(String key){
        if ("main".equalsIgnoreCase(key)){
            return;
        }
        Stage stage = cache.get(key);
        if (null != stage){
            cache.del(key);
        }
        mainShow();
        Platform.runLater(() -> {
            if (null != stage){
                stage.close();
            }
        });
    }

    public void mainShow(){
        if (cache.getSize() == 1){
            Stage main = cache.get("main");
            Platform.runLater(() -> {
                main.show();
            });
        }
    }
}
